package com.dmxtechnologies.community.rar.outbound;

import java.util.logging.Logger;

import javax.resource.ResourceException;

import com.dmxtechnologies.community.enums.CMSRequestHead;
import com.dmxtechnologies.community.pojo.CMSDetailResponse;
import com.dmxtechnologies.community.rar.api.CMSBaseRequest;
import com.dmxtechnologies.community.rar.api.CMSBaseResponse;
import com.dmxtechnologies.community.rar.api.CMSConnection;
import com.dmxtechnologies.community.rar.api.CMSProcessingException;

/* An application-level connection handle used by clients to access
 * the physical connection. The physical connection is represented by
 * a ManagedConnection instance */
public class CMSConnectionImpl implements CMSConnection {
    
    private static final Logger log = Logger.getLogger("CMSConnectionImpl");
    private CMSManagedConnection mconnection;
    private boolean valid;
    private  String smallPicURL;
    private  String srcURl;
	
    /* */
	CMSConnectionImpl(CMSManagedConnection mconnection) {
		this.mconnection = mconnection;
		valid = true;
		smallPicURL = "http://"+ mconnection.getHost()+":"+mconnection.getPort()+"/"+mconnection.getProjectName()+"/res/content/small/";
		srcURl = "http://"+ mconnection.getHost()+":"+mconnection.getPort()+"/"+mconnection.getProjectName()+"/";
	}

	/* Called by the managed connection to dis/associate this handle. */
	CMSManagedConnection getManagedConnection() {
		return mconnection;
	}

	void setManagedConnection(CMSManagedConnection mconnection) {
		this.mconnection = mconnection;
	}

	/* Called by the managed connection to invalidate this handle */
	void invalidate() {
		valid = false;
	}

	/* Closes the connection handle */
	@Override
	public void close() throws ResourceException {
		log.info("[CMSConnectionImpl] close()");
		valid = false;
		mconnection.disassociateConnection();
	}
	
    /* Submits a request to the EIS */
	@Override
	public CMSBaseResponse getListRequestToCMS(CMSBaseRequest request) throws CMSProcessingException {
		log.info("[CMSConnectionImpl] submitRequestToCMS()");
		String url = mconnection.getInterfaceName()+"?key="+CMSRequestHead.L+"_"+request.getCommunity()+"_"+request.getPreColumns()
				+"_"+request.getTemplateExample()+"_"+request.getNextColumns()+"_"+request.getPagination();
		if (valid) {
			//json字符串
			String resp = mconnection.sendRequestToEIS(srcURl+url);
			return new CMSBaseResponse(resp,smallPicURL);
		} else {
			throw new CMSProcessingException("Connection handle is invalid");
		}
	}

	@Override
	public CMSBaseResponse getContentDetailRequestToCMS(CMSBaseRequest request)
			throws CMSProcessingException {
		log.info("[CMSConnectionImpl] getContentDetailRequestToCMS()");
		String url = mconnection.getInterfaceName()+"?key="+CMSRequestHead.C+"_"+request.getCommunity()+"_"+request.getPreColumns()
				+"_"+request.getTemplateExample()+"_"+request.getNextColumns()+"_"+request.getPagination();
		if (valid) {
			//json字符串
			String resp = mconnection.sendRequestToEIS(srcURl+url);
			return new CMSDetailResponse(resp,smallPicURL);
		} else {
			throw new CMSProcessingException("Connection handle is invalid");
		}
	}
}
